package com.chen.blue.controller.admin.system;


import com.chen.blue.domain.delete.system.RoleDelete;
import com.chen.blue.domain.delete.system.UserDelete;
import com.chen.blue.domain.dto.system.RoleDto;
import com.chen.blue.domain.dto.system.UserDto;
import com.chen.blue.domain.dto.system.update.RoleDtoEdit;
import com.chen.blue.domain.dto.system.update.UserDtoEdit;
import com.chen.blue.domain.query.system.RoleQuery;
import com.chen.blue.domain.query.system.UserQuery;
import com.chen.blue.domain.vo.system.RoleVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.exception.e.NoDeleteException;
import com.chen.blue.result.DataResult;
import com.chen.blue.result.R;
import com.chen.blue.service.system.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author CLT
 * @since 2024-04-27
 */
@Api(tags = "角色模块")
@Validated
@RestController
@RequestMapping("/role")
@PreAuthorize("hasRole('ROLE_admin')")
public class RoleController {

    @Resource
    private IRoleService iRoleService;

    @ApiOperation(value = "角色添加", notes = "角色添加")
    @PostMapping("/add")
    public R add(@RequestBody @Valid RoleDto roleDto) throws DtoNullException {

        return iRoleService.addRole(roleDto) ? R.success() : R.faild();

    }

    @ApiOperation(value = "角色删除", notes = "角色删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody RoleDelete roleDelete) throws DtoNullException, NoDeleteException {

        return iRoleService.deleteRole(roleDelete) ? R.success() : R.faild();

    }

    @ApiOperation(value = "角色修改", notes = "角色修改")
    @PutMapping("/edit")
    public R updateById(@RequestBody @Valid RoleDtoEdit roleDtoEdit) throws DtoNullException {

        return iRoleService.updateRoleById(roleDtoEdit) ? R.success() : R.faild();

    }

    @ApiOperation(value = "多条件查询角色", notes = "多条件查询角色")
    @PostMapping("/query")
    public R<DataResult<List<RoleVo>>> query(@RequestBody RoleQuery roleQuery) throws DtoNullException {

        return R.success(iRoleService.queryRole(roleQuery));

    }

    @ApiOperation(value = "ID查询角色", notes = "ID查询角色")
    @GetMapping("/queryById/{id}")
    public R<DataResult<RoleVo>> queryById(@PathVariable("id") Long id) throws DtoNullException {

        return R.success(iRoleService.queryRoleById(id));

    }

}

